<template>
  <div class="makeapie-box">
    <div class="makeapie-box-viewer" v-for="image in images" :key="image.id">
      <el-image :src="image.url"></el-image>
      <div>
        <p>{{ image.name }}</p>
        <el-button @click="download(image.id)">下载模板</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      url: "",
      images: [],
      list: [],
    };
  },
  mounted() {
    this.url = "http://82.156.162.96";
    //this.url = "http://localhost:8080";
    const that = this;
    axios.get(this.url + "/makeapie/list.txt").then(function (res) {
      if (res.status != 200) {
        return;
      }
      that.list = res.data.split("\r\n");
      that.updateList(0, 20);
    });

    this.$nextTick(() => {
      const el = document.querySelector(".makeapie-box");
      const offsetHeight = el.offsetHeight;
      el.onscroll = () => {
        const scrollTop = el.scrollTop;
        const scrollHeight = el.scrollHeight;
        if (offsetHeight + scrollTop - scrollHeight >= -1) {
          this.updateList(this.images.length, this.images.length + 20);
        }
      };
    });
  },
  methods: {
    updateList(start, end) {
      for (var i = start; i < end; i++) {
        let errflag;
        let key;
        if (this.list[i].split("----").length > 1) {
          key = this.list[i].split("----");
          errflag = true;
        } else {
          key = this.list[i].split("---");
          errflag = false;
        }
        let id = errflag ? key[0] + "-" : key[0];
        this.images.push({
          id: id,
          name: key[1],
          url: this.url + "/makeapie/images/" + id + ".png",
        });
      }
    },
    download(id) {
      axios
        .get(this.url + "/makeapie/pages/" + id + ".js", {
          responseType: "blob",
        })
        .then((res) => {
          let url = window.URL.createObjectURL(
            new Blob([res.data], {
              type: "application/octet-stream",
            })
          );

          let link = document.createElement("a");
          link.download = id + ".js";
          link.href = url;
          link.click();
          URL.revokeObjectURL(url);
        });
    },
  },
};
</script>

<style>
.makeapie-box {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding-bottom: 40px;
  overflow-y: auto;
}

.makeapie-box div {
  border-radius: 5px;
}

.makeapie-box-viewer {
  float: left;
  position: relative;
  margin-top: 40px;
  margin-left: 40px;
  width: 320px;
  height: 240px;
  background: white;
}

.makeapie-box-viewer div:nth-child(2) {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.95) 0%,
    rgba(255, 255, 255, 0.88) 33%,
    rgba(255, 255, 255, 0.65) 100%
  );
  transition: 0.1s all ease-in-out;
  opacity: 0;
}

.makeapie-box-viewer div:nth-child(2):hover {
  opacity: 1;
}

.makeapie-box-viewer p {
  margin-top: 20px;
  margin-left: 20px;
  width: 280px;
  font-size: 18px;
}

.makeapie-box-viewer button {
  position: absolute;
  right: 20px;
  bottom: 20px;
  font-size: 14px;
}

.makeapie-box-viewer img {
  width: 100%;
}
</style>
